define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                     shop_id:0,
                     shop_info:{},
                     shoppingCartList:{},
                     empty:0,
                     data:{}

                };
            },
            el: '#pageCart',
            created: function() {
                this.fetchData();
            },
            mounted: function() {
                    
                    

            },

            methods: {
                fetchData: function() {
                    var _this = this;
                    
                    this.shop_id = this.$route.params.shop_id;
                    this.getcartByUserId();
                },
                 // 购物车
                getcartByUserId: function () {
                  var that = this;
                  app.get("mall/cart/getcartByUserId", { shop_id: this.shop_id }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                    } else if (res.data) {
                      that.setData({ shoppingCartList: res.data ,shop_info:res.data.shop_info});
                    }
                    if (res.data.count===0){
                      that.setData({"empty":1});
                    }
                  });
                },
                setData:function(arr){
                  for( var i in arr){
                    // console.log(i);
                    // console.log(arr[i]);
                     // this.$set(i,arr[i]); 
                     this[i] = arr[i];
                  }
                },

                //商品数量减
                minus: function (e) {
                  var target = $(e.target);
                  var id = target.attr("data-id");
                  var shoppingCartList = this.shoppingCartList;
                  if (shoppingCartList.list[id].count < 1) {
                    shoppingCartList.list[id].count = 0;
                  } else {
                    shoppingCartList.list[id].count--;
                  }
                  this.setData({ shoppingCartList: shoppingCartList });
                  this.addToCart({
                    count: shoppingCartList.list[id].count,
                    goods_spec_id:target.attr("data-goods_spec_id"),//  $(input).attr("goods_spec_id"),
                    goods_id: target.attr("data-goods_id"),
                  });

                },
                //商品数量加
                plus: function (e) {
                   var target = $(e.target);
                  var id = target.attr("data-id");
                 
                  var shoppingCartList = this.shoppingCartList;
                  shoppingCartList.list[id].count++;
                  this.setData({ shoppingCartList: shoppingCartList });
                  this.addToCart({
                    count: shoppingCartList.list[id].count,
                    goods_spec_id:target.attr("data-goods_spec_id"),//  $(input).attr("goods_spec_id"),
                    goods_id: target.attr("data-goods_id"),
                  });
                },
                //商品数量
                changeCount: function (e) {

                   var target = $(e.target);
                  var id = target.attr("data-id");
                  var count = target.val();
                  var shoppingCartList = this.shoppingCartList;
                  shoppingCartList.list[id].count = count;
                  this.setData({ shoppingCartList: shoppingCartList });
                  this.addToCart({
                    count: count,
                     goods_spec_id:target.attr("data-goods_spec_id"),//  $(input).attr("goods_spec_id"),
                    goods_id: target.attr("data-goods_id"),
                  });

                },

                //加入购物车
                addToCart: function (post, callback) {
                  
                  var that = this;
                  app.get("/mall/cart/add", post, function (res) {
                    
                    if (res.code != 200) {
                      app.alert(res.msg);
                    } else {
                      callback ? callback(res) : '';
                      that.setData({ shoppingCartList: res.data });

                    }
                  });
                },
                //清空购物车
                clearCart: function () {
                
                  var that = this;
                  app.confirm({
                     text:'确定清空购物车吗？',
                     yes:function(){
                          app.get("mall/cart/clearCart", { shop_id: that.shop_id }, function (res) {
                          if (res.code != 200) {
                            app.alert(res.msg);
                          } else {
                            //phpkitApp.alert("恭喜,成功清空购物车!");
                            app.alert('恭喜,成功清空购物车!');
                            that.setData({ shoppingCartList: {
                              nextText:'去结算'
                            } });
                          }
                         
                        });
                     }
                  });
                  
                },
                //去结算
                settle_accounts: function () {
                  this.$router.push({name:'checkout',params:{shop_id:this.shop_id}});
                },
 
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
